#!/usr/bin/env python
# -*- coding: utf-8 -*-

from teamupshoppers     import app
from flask              import Flask, views, render_template, request, session, flash, url_for, redirect, g, Response
from flask.ext.login    import login_required
from db                 import DB
import logging, json
from datetime import date

"""Manage notice. """
class Notice(views.MethodView):
    @login_required
    def get(self, notice_id):
        if notice_id is None:
            logging.warning('LOG: DataHandler::get ')
            feedback = self._get_all()
        else:
            logging.warning('LOG: DataHandler::get ' + notice_id)
            feedback = self._get_by(notice_id)
        return Response(json.dumps(feedback),
                        mimetype='application/json' )
    def _get_all(self):
        return DB().query("SELECT   notice.id, notice.name, notice.shop_name, notice.descr, notice.planned_day_of_order "
                  "FROM     notice, notice_user "
                  "WHERE    notice_user.user_id = ?"
                  "AND      notice.id = notice_user.notice_id",
                  [session["user_id"]])
    def _get_by(self, notice_id):
        return DB().query("SELECT   notice.id, notice.name, notice.shop_name, notice.descr, notice.planned_day_of_order "
                  "FROM     notice, notice_user "
                  "WHERE    notice_user.user_id = ? "
                  "AND      notice.id = notice_user.notice_id "
                  "AND      notice.id = ? ",
                  [session["user_id"],notice_id])
        
    @login_required
    def post(self):
        logging.warning('LOG: DataHandler::post ' + str(request.json) )        
        required = ["name","shop_name","descr","planned_day_of_order"]
        for r in required:
            if r not in request.json:
                flash("Error: {0} is required.".format(r))
                return redirect(url_for("main"))
        error = ""       
        sql_insert_notice = "INSERT INTO notice (name,shop_name,descr,planned_day_of_order,date_create ) VALUES (?,?,?,?,?)"
                    
        notice_id = DB().insert(sql_insert_notice,
                                [request.json["name"],
                                 request.json["shop_name"],
                                 request.json["descr"],
                                 date.today(),
                                 date.today()
                                ])
        sql_insert_notice_user = "INSERT INTO notice_user (notice_id, user_id ) VALUES (?,?)"
                    
        DB().insert(sql_insert_notice_user,
                                [notice_id,
                                 session["user_id"]
                                ])
        feedback = self._get_by(notice_id)
        logging.warning('LOG: DataHandler::post ' + str(feedback) )        
        return Response(json.dumps(feedback),
                        mimetype='application/json' )

    @login_required
    def delete(self, notice_id):
        logging.warning('LOG: DataHandler::delete ' + notice_id)

    @login_required
    def put(self, notice_id):
        logging.warning('LOG: DataHandler::put ' + notice_id)
